﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.InputOutput.Encryption
Partial Class AnnouncementList
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 10
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadAnnouncement()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadAnnouncement()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadAnnouncement()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadAnnouncement()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
         LoadAnnouncement()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadAnnouncement()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
         LoadAnnouncement()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
         LoadAnnouncement()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
         LoadAnnouncement()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadAnnouncement()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadAnnouncement()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
             LoadAnnouncement()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
         LoadAnnouncement()
    End Sub

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        ElseIf Session.Item("StaffType").ToString = "2" Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If

        If Not Page.IsPostBack Then
            LoadStaff()
            LoadAnnouncement()
        End If

        If Not Session.Item("SavedAnnouce") Is Nothing Then
            If Session.Item("SavedAnnouce").ToString = "Y" Then
                lblMsgSuccessful.Text = "Annoucement Saved"
                divSuccessful.Visible = True
            End If

        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")
            '0-Back Page
            '1-title
            '2-Create By
            '3-Priority
            '4-Date From
            '5-Date To
            '6-Page Size
            '7-Current Page
            If arr(0).ToString.Contains("Announcementlist.aspx") Then
                txtTitle.Text = arr(1).ToString
                ddlCreateBy.SelectedValue = arr(2).ToString
                ddlPriority.SelectedValue = arr(3).ToString
                txtFromDate.Text = arr(4).ToString
                txtToDate.Text = arr(5).ToString
                PageSize = CInt(arr(6).ToString)
                CurrentPage = CInt(arr(7).ToString)
                Session.Remove("Back")
                LoadAnnouncement()
            Else
                Session.Remove("Back")
            End If
           
        End If
    End Sub

    Private Sub LoadStaff()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Try
            objDB.OpenConnection()
            objDB.dbVariable.SQLString = "SELECT Name,StaffId FROM AW_Staff ORDER BY Name"
            objDB.FillData(TempDT, "AW_Staff", Database.SQLCommandType.NormalString)
            objDB.CloseConnection()

            ddlCreateBy.DataSource = TempDT
            ddlCreateBy.DataTextField = "Name"
            ddlCreateBy.DataValueField = "StaffId"
            ddlCreateBy.DataBind()
            ddlCreateBy.Items.Insert(0, "-SELECT-")
            ddlCreateBy.Items(0).Value = ""
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub

    Private Sub LoadAnnouncement()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        Dim strTitle, strCreateBy, strPriority, strDate As String

        Try
            objDB.OpenConnection()

            If txtTitle.Text = String.Empty Then
                strTitle = ""
            Else
                strTitle = " AND Title LIKE '%" & txtTitle.Text.Trim & "%'"
            End If

            If ddlCreateBy.SelectedValue = "" Then
                strCreateBy = ""
            Else
                strCreateBy = " AND (CreateBy = @CreateBy or UpdateBy = @CreateBy)"
                objDB.AddParameter("CreateBy", Data.SqlDbType.UniqueIdentifier, ddlCreateBy.SelectedValue)
            End If

            If ddlPriority.SelectedValue = "" Then
                strPriority = ""
            Else
                strPriority = " AND Priority = @Priority"
                objDB.AddParameter("Priority", Data.SqlDbType.TinyInt, CInt(ddlPriority.SelectedValue))
            End If

            If txtFromDate.Text = String.Empty And txtToDate.Text = String.Empty Then
                strDate = ""
            ElseIf txtFromDate.Text = String.Empty Then
                strDate = " AND a.CreateDate = @CreateDate"
                objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, DateTime.ParseExact(txtToDate.Text, "dd-MMM-yyyy", Nothing))
            ElseIf txtToDate.Text = String.Empty Then
                strDate = " AND a.CreateDate = @CreateDate"
                objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, DateTime.ParseExact(txtFromDate.Text, "dd-MMM-yyyy", Nothing))
            Else
                strDate = " AND (a.CreateDate BETWEEN @FromDate AND @ToDate AND a.UpdateDate IS NULL) OR (a.UpdateDate BETWEEN @FromDate AND @ToDate)"
                objDB.AddParameter("FromDate", Data.SqlDbType.DateTime, DateTime.ParseExact(txtFromDate.Text, "dd-MMM-yyyy", Nothing))
                objDB.AddParameter("ToDate", Data.SqlDbType.DateTime, DateTime.ParseExact(txtToDate.Text, "dd-MMM-yyyy", Nothing).AddDays(1))
            End If

            objDB.dbVariable.SQLString = "SELECT a.AnnouncementId, a.Title, CASE a.Priority WHEN 1 THEN 'HIGH' WHEN 2 THEN 'MEDIUM' ELSE 'LOW' END as Priority, " & _
                                        "CASE WHEN u.Initial IS NULL THEN c.Initial ELSE u.Initial END as CreateBy, " & _
                                        "CASE WHEN a.UpdateDate IS NULL THEN a.CreateDate ELSE a.UpdateDate END as CreateDate FROM AW_Announcement a LEFT JOIN " & _
                                        "AW_Staff u ON a.UpdateBy = u.StaffId LEFT JOIN AW_Staff c ON a.CreateBy = c.StaffId WHERE a.StatusDelete = 0 " & _
                                        strTitle & strCreateBy & strPriority & strDate & lblHidSortType.Text
            objDB.FillData(TempDT, "AW_Announcement", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()

            'Creating a Paged Datasouce
            Dim objPaged As New PagedDataSource
            objPaged.DataSource = TempDT.DefaultView
            objPaged.AllowPaging = True
            objPaged.PageSize = PageSize
            objPaged.CurrentPageIndex = CurrentPage

            lblHeadNoOfItem.Text = TempDT.Rows.Count
            lblFootNoOfItem.Text = TempDT.Rows.Count

            repAnnouncement.DataSource = objPaged
            repAnnouncement.DataBind()

            lblMaxPageCount.Text = objPaged.PageCount - 1
            lblTotalPage.Text = " of " & objPaged.PageCount
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadAnnouncement()
    End Sub

    Protected Sub imgNewAnnouncement_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgNewAnnouncement.Click
        Response.Redirect("announcemententry.aspx")
    End Sub

    Protected Sub repAnnouncement_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repAnnouncement.ItemCommand

        If e.CommandName.ToUpper = "EDITITEM" Then
            'Edited by minginn - To allow user to go back to the search page with same search selection
            'Back Page
            'title
            'Create By
            'Priority
            'Date From
            'Date To
            'Page Size
            'Current Page
            Dim strBack As String
            strBack = "Announcementlist.aspx" & "ڡ" & txtTitle.Text.Trim & "ڡ" & ddlCreateBy.SelectedValue & "ڡ" & _
                    ddlPriority.SelectedValue & "ڡ" & txtFromDate.Text.Trim & "ڡ" & txtToDate.Text.Trim & "ڡ" & _
                    PageSize & "ڡ" & CurrentPage
            Session.Add("Back", strBack)
            Dim AnnouncementUID As String = CType(repAnnouncement.Items(e.CommandArgument).FindControl("lblRepAnnouncementUID"), Label).Text
            Response.Redirect("AnnouncementEntry.aspx?id=" & SymmetricEncryptToHex(AnnouncementUID, "augment"))
        Else
            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE AW_Announcement SET StatusDelete=1 WHERE AnnouncementId=@AnnouncementId"
                objDB.AddParameter("AnnouncementId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repAnnouncement.Items(e.CommandArgument).FindControl("lblRepAnnouncementUID"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
                lblMsgSuccessful.Text = "Announcement Entry Deleted"
                lblMsgSuccessful.Visible = True
                LoadAnnouncement()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End If
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkDate.Click
        If lnkDate.Text = "Date▼" Then
            lnkDate.Text = "Date▲"
        Else
            lnkDate.Text = "Date▼"
        End If

        lblHidSortType.Text = " ORDER BY CreateDate " & SortType(lnkDate.Text) & ", Title " & SortType(lnkTitle.Text) & _
                            ", Priority " & SortType(lnkPriority.Text) & ", CreateBy " & SortType(lnkCreateBy.Text)
        LoadAnnouncement()
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkTitle.Click
        If lnkTitle.Text = "Title▼" Then
            lnkTitle.Text = "Title▲"
        Else
            lnkTitle.Text = "Title▼"
        End If

        lblHidSortType.Text = " ORDER BY Title " & SortType(lnkTitle.Text) & ", CreateDate " & SortType(lnkDate.Text) & _
                            ", Priority " & SortType(lnkPriority.Text) & ", CreateBy " & SortType(lnkCreateBy.Text)
        LoadAnnouncement()
    End Sub

    Protected Sub lnkPriority_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPriority.Click
        If lnkPriority.Text = "Priority▼" Then
            lnkPriority.Text = "Priority▲"
        Else
            lnkPriority.Text = "Priority▼"
        End If

        lblHidSortType.Text = " ORDER BY Priority " & SortType(lnkPriority.Text) & ", CreateDate " & SortType(lnkDate.Text) & _
                            ", Title " & SortType(lnkTitle.Text) & ", CreateBy " & SortType(lnkCreateBy.Text)
        LoadAnnouncement()
    End Sub

    Protected Sub lnkCreateBy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCreateBy.Click
        If lnkCreateBy.Text = "By▼" Then
            lnkCreateBy.Text = "By▲"
        Else
            lnkCreateBy.Text = "By▼"
        End If

        lblHidSortType.Text = " ORDER BY CreateBy " & SortType(lnkCreateBy.Text) & ", CreateDate " & SortType(lnkDate.Text) & _
                            ", Title " & SortType(lnkTitle.Text) & ", Priority " & SortType(lnkPriority.Text)
        LoadAnnouncement()
    End Sub
End Class
